package com.study.condition;

import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.context.annotation.Condition;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.env.Environment;
import org.springframework.core.type.AnnotatedTypeMetadata;

/**
 * @author wuwenbo
 * @date 2020/3/27 - 20:36
 */
public class LinuxConditional implements Condition {
    /**
     * ConditionContext 判断条件能使用的上下文(环境)
     * AnnotatedTypeMetadata 注释信息
     *
     */
    public boolean matches(ConditionContext conditionContext, AnnotatedTypeMetadata annotatedTypeMetadata) {
        //是否linux系统
        //1.能获取到 ioc 使用的bean 工厂
        ConfigurableBeanFactory configurableBeanFactory =  conditionContext.getBeanFactory();
        //2.获取类加载器
        ClassLoader classLoader =   conditionContext.getClassLoader();
        //3.获取当前环境信息
        Environment environment = conditionContext.getEnvironment();
        //4.获取到bean定义的注册类
        BeanDefinitionRegistry registry =  conditionContext.getRegistry();

        String property = environment.getProperty("os.name");
        if (property.contains("linux")) {
            return true;
        }
        return false;
    }
}
